package xddq

import (
	"fmt"
	"math/rand"
	"time"
	D "websocket_apn/define"
	"websocket_apn/log"
	"websocket_apn/proto"
)

// 运势任务
var FortuneTaskMap = map[int]D.FortuneTaskMap{
	//灵兽运势 91-A1 4E
	10001: {ID: "914E", Name: "锤炼200次", Count: 200},
	10002: {ID: "924E", Name: "锤炼500次", Count: 500},
	10003: {ID: "934E", Name: "升级灵兽1次", Count: 1},
	10004: {ID: "944E", Name: "升级灵兽2次", Count: 2},
	10005: {ID: "954E", Name: "升级灵兽3次", Count: 3},
	10006: {ID: "964E", Name: "升级灵兽5次", Count: 5},
	10007: {ID: "974E", Name: "召唤灵兽1次", Count: 1},
	10008: {ID: "984E", Name: "召唤灵兽2次", Count: 2},
	10009: {ID: "994E", Name: "召唤灵兽3次", Count: 3},
	10010: {ID: "9A4E", Name: "分享1次", Count: 1},
	10011: {ID: "9B4E", Name: "登入游戏圈1次", Count: 1},
	10012: {ID: "9C4E", Name: "论坛签到1次", Count: 1},
	10013: {ID: "9D4E", Name: "抽奖30次", Count: 30},
	10014: {ID: "9E4E", Name: "抽奖50次", Count: 50},
	10015: {ID: "9F4E", Name: "抽奖100次", Count: 100},
	10016: {ID: "A04E", Name: "抽奖120次", Count: 120},
	10017: {ID: "A14E", Name: "锤炼200次", Count: 200},
	//精怪运势 A2-B2 4E
	10018: {ID: "A24E", Name: "锤炼200次", Count: 200},
	10019: {ID: "A34E", Name: "锤炼500次", Count: 500},
	10020: {ID: "A44E", Name: "吸引精怪1次", Count: 1},
	10021: {ID: "A54E", Name: "吸引精怪2次", Count: 2},
	10022: {ID: "A64E", Name: "吸引精怪3次", Count: 3},
	10023: {ID: "A74E", Name: "吸引精怪5次", Count: 5},
	10024: {ID: "A84E", Name: "吸引精怪7次", Count: 7},
	10025: {ID: "A94E", Name: "吸引精怪9次", Count: 9},
	10026: {ID: "AA4E", Name: "吸引精怪12次", Count: 12},
	10027: {ID: "AB4E", Name: "分享1次", Count: 1},
	10028: {ID: "AC4E", Name: "登入游戏圈1次", Count: 1},
	10029: {ID: "AD4E", Name: "论坛签到1次", Count: 1},
	10030: {ID: "AE4E", Name: "抽奖30次", Count: 30},
	10031: {ID: "AF4E", Name: "抽奖50次", Count: 50},
	10032: {ID: "B04E", Name: "抽奖100次", Count: 100},
	10033: {ID: "B14E", Name: "抽奖120次", Count: 120},
	10034: {ID: "B24E", Name: "抽奖200次", Count: 200},
	//神通运势 B3-C3 4E
	10035: {ID: "B34E", Name: "锤炼200次", Count: 200},
	10036: {ID: "B44E", Name: "锤炼500次", Count: 500},
	10037: {ID: "B54E", Name: "神通升级1次", Count: 1},
	10038: {ID: "B64E", Name: "神通升级3次", Count: 3},
	10039: {ID: "B74E", Name: "神通升级5次", Count: 5},
	10040: {ID: "B84E", Name: "神通衍化3次", Count: 3},
	10041: {ID: "B94E", Name: "神通衍化5次", Count: 5},
	10042: {ID: "BA4E", Name: "神通衍化7次", Count: 7},
	10043: {ID: "BB4E", Name: "神通衍化10次", Count: 10},
	10044: {ID: "BC4E", Name: "分享1次", Count: 1},
	10045: {ID: "BD4E", Name: "登入游戏圈1次", Count: 1},
	10046: {ID: "BE4E", Name: "论坛签到1次", Count: 1},
	10047: {ID: "BF4E", Name: "抽奖30次", Count: 30},
	10048: {ID: "C04E", Name: "抽奖50次", Count: 50},
	10049: {ID: "C14E", Name: "抽奖100次", Count: 100},
	10050: {ID: "C24E", Name: "抽奖120次", Count: 120},
	10051: {ID: "C34E", Name: "抽奖200次", Count: 200},
	//内丹运势 C4-D4 4E
	10052: {ID: "C44E", Name: "锤炼200次", Count: 200},
	10053: {ID: "C54E", Name: "锤炼500次", Count: 500},
	10054: {ID: "C64E", Name: "吸引精怪2次", Count: 2},
	10055: {ID: "C74E", Name: "吸引精怪3次", Count: 3},
	10056: {ID: "C84E", Name: "吸引精怪4次", Count: 5},
	10057: {ID: "C94E", Name: "吸引精怪5次", Count: 7},
	10058: {ID: "CA4E", Name: "吸引精怪10次", Count: 1},
	10059: {ID: "CB4E", Name: "吸引精怪10次", Count: 12},
	10060: {ID: "CC4E", Name: "吸引精怪12次", Count: 12},
	10061: {ID: "CD4E", Name: "分享1次", Count: 1},
	10062: {ID: "CE4E", Name: "登入游戏圈1次", Count: 1},
	10063: {ID: "CF4E", Name: "论坛签到1次", Count: 1},
	10064: {ID: "D04E", Name: "抽奖30次", Count: 30},
	10065: {ID: "D14E", Name: "抽奖50次", Count: 50},
	10066: {ID: "D24E", Name: "抽奖100次", Count: 100},
	10067: {ID: "D34E", Name: "抽奖120次", Count: 120},
	10068: {ID: "D44E", Name: "抽奖200次", Count: 200},
	//法宝运势 D5-E5 4E
	10069: {ID: "D54E", Name: "锤炼200次", Count: 200},
	10070: {ID: "D64E", Name: "锤炼500次", Count: 500},
	10071: {ID: "D74E", Name: "寻法宝1次", Count: 1},
	10072: {ID: "D84E", Name: "寻法宝2次", Count: 3},
	10073: {ID: "D94E", Name: "寻法宝5次", Count: 5},
	10074: {ID: "DA4E", Name: "寻法宝7次", Count: 7},
	10075: {ID: "DB4E", Name: "炼化法宝1次", Count: 1},
	10076: {ID: "DC4E", Name: "炼化法宝3次", Count: 3},
	10077: {ID: "DD4E", Name: "炼化法宝5次", Count: 5},
	10078: {ID: "DE4E", Name: "分享1次", Count: 1},
	10079: {ID: "DF4E", Name: "登入游戏圈1次", Count: 1},
	10080: {ID: "E04E", Name: "论坛签到1次", Count: 1},
	10081: {ID: "E14E", Name: "抽奖30次", Count: 30},
	10082: {ID: "E24E", Name: "抽奖50次", Count: 50},
	10083: {ID: "E34E", Name: "抽奖100次", Count: 100},
	10084: {ID: "E44E", Name: "抽奖120次", Count: 120},
	10085: {ID: "E54E", Name: "抽奖200次", Count: 200},
	//法宝运势 D5-E5 4E
	10100: {ID: "F44E", Name: "锤炼200次", Count: 200},
	10101: {ID: "F54E", Name: "锤炼500次", Count: 500},
	10102: {ID: "F64E", Name: "寻法宝1次", Count: 1},
	10103: {ID: "F74E", Name: "寻法宝2次", Count: 3},
	10104: {ID: "F84E", Name: "寻法宝5次", Count: 5},
	10105: {ID: "F94E", Name: "寻法宝7次", Count: 7},
	10106: {ID: "FA4E", Name: "炼化法宝1次", Count: 1},
	10107: {ID: "FB4E", Name: "炼化法宝3次", Count: 3},
	10108: {ID: "FC4E", Name: "炼化法宝5次", Count: 5},
	10109: {ID: "FD4E", Name: "分享1次", Count: 1},
	10110: {ID: "FE4E", Name: "登入游戏圈1次", Count: 1},
	10111: {ID: "FF4E", Name: "论坛签到1次", Count: 1},
	10112: {ID: "DF4E", Name: "抽奖30次", Count: 30},
	10113: {ID: "E04E", Name: "抽奖50次", Count: 50},
	10114: {ID: "E14E", Name: "抽奖100次", Count: 100},
	10115: {ID: "E24E", Name: "抽奖120次", Count: 120},
	10116: {ID: "E34E", Name: "抽奖200次", Count: 200},
}

// 运势活动描述
var FortuneTypeDesc = []string{
	" ",
	"灵兽运势", // 91-A1 4E
	"精怪运势", // A2-B2 4E
	"神通运势", // B3-C3 4E
	"内丹运势", // C4-D4 4E
	"法宝运势", // D5-E5 4E
	"未知运势", //
}

// 获取低等级随机神通ID
func GetRandomMagicDerivationID() string {
	//绿 蓝 紫 橙
	data := []string{
		//道法
		"B309", //黄沙阵
		"B209", //掌心雷
		"B109", //身外身法
		"B009", //定身咒
		"CE08", //落石术
		"CD08", //借力术
		"CC08", //脱胎换骨
		//神识
		"9B11", //清心妙法
		"9A11", //拘神
		"9911", //附灵术
		"9811", //识海钉
		"B610", //呼名夺魂
		"B510", //灵魂啸
		"B410", //神识刺
		//驭兽
		"8319", //践踏
		"8219", //暗囊
		"8119", //扰乱
		"8019", //回春
		"9E18", //纵越
		"9D18", //共鸣
		"9C18", //舔
		//躯体
		"EB20", //麒麟臂
		"EA20", //莲花化身
		"E920", //天媚体
		"E820", //大魔邪身
		"8620", //化羽
		"8520", //铜皮铁骨
		"8420", //长生体
	}
	rand.Seed(time.Now().UnixNano())
	randNum := rand.Intn(len(data))
	return data[randNum]
}

// 获取低等级随机法宝ID
func GetRandomMagicTreasureID() string {
	//凡界神话 40001	C1 B8 02 39个
	//凡界传说 30001	B1 6A 02 98个
	//凡界卓越 20001	A1 9C 01 50个
	//凡界普通 10001	91 4E    20个

	//灵界神话 140001 E1 C5 08 5个
	//灵界传说 130001 D1 F7 07 45个
	//灵界卓越 120001 C1 A9 07 30个
	//灵界普通 110001 B1 DB 06 20个
	rand.Seed(time.Now().UnixNano())
	randNum := int64(rand.Intn(40))
	ID := int64(0)
	if randNum < 20 {
		ID = 10001 + randNum
	} else {
		ID = 110001 + randNum - 20
	}
	return proto.BytesToHexStr(proto.IntDataToBytes(ID))
}

// 获取背包中低等级随机灵兽ID
func (xd *XdDriver) GetRandomPackPetID() string {
	var data []string
	for _, info := range xd.UserData.PackPet {
		if info.Level < 10 && info.ID[1] == 0xE3 {
			data = append(data, info.Idx)
		}
	}
	if len(data) == 0 {
		return ""
	}
	rand.Seed(time.Now().UnixNano())
	randNum := rand.Intn(len(data))
	return data[randNum]
}

/*
 */

func (xd *XdDriver) CheckActivityDataInfoData(allInfo D.ActivityDataInfo) {
	if allInfo.FortuneType > 0 && len(allInfo.FortuneTask) > 0 {
		fortuneName := FortuneTypeDesc[allInfo.FortuneType]
		giftData := ""
		for _, task := range allInfo.FortuneTask {
			log.Infof("%s-[%s]已完成%d次", fortuneName, FortuneTaskMap[task.IDi].Name, task.Count)
			if tmp, ok := FortuneTaskMap[task.IDi]; ok {
				tmp.Num = task.Count
				tmp.Done = task.Done
				tmp.Take = task.Take
				FortuneTaskMap[task.IDi] = tmp
			}
			if task.Take {
				continue
			}
			if task.Done {
				giftData += fmt.Sprintf("0A 07 08 %s 10 %s ", xd.UserData.Activity[D.Activity_Fortune].ID, task.ID)
			}
		}
		if len(giftData) > 0 {
			log.Infof("%s-领取已完成任务", fortuneName)
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_ACTIVITY_GET_CONDITION_REWARD_BY_ARR, giftData))
		}
		taskID := 0
		taskCount := int64(0)
		i := int64(0)
		startTaskID := 10001
		switch allInfo.FortuneType {
		case D.A_Fortune_Ling_Shou:
			startTaskID = 10001
			taskID = 10006 //升级灵兽
			taskCount = FortuneTaskMap[taskID].Count - FortuneTaskMap[taskID].Num
			if !FortuneTaskMap[taskID].Take && taskCount > 0 {
				log.Infof("%s-[%s]已完成%d次", fortuneName, FortuneTaskMap[taskID].Name, FortuneTaskMap[taskID].Num)
				if len(xd.UserData.PackPet) > 0 {
					for i = 0; i < taskCount; i++ {
						time.Sleep(3 * time.Second)
						xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PET_LEVEL_UP, "08 "+xd.GetRandomPackPetID()))
					}
				}
			}
			taskID = 10009 //召唤灵兽
			xd.UserData.PetCall = FortuneTaskMap[taskID].Count - FortuneTaskMap[taskID].Num
		case D.R_Resource_Jing_Guai:
			startTaskID = 10018
			taskID = 10026 //吸引精怪
			taskCount = FortuneTaskMap[taskID].Count - FortuneTaskMap[taskID].Num
			if !FortuneTaskMap[taskID].Take && taskCount > 0 {
				log.Infof("%s-[%s]已完成%d次", fortuneName, FortuneTaskMap[taskID].Name, FortuneTaskMap[taskID].Num)
				for i = 0; i < taskCount; i++ {
					time.Sleep(3 * time.Second)
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_SPIRIT_DRAW, "08 01"))
				}
			}
		case D.R_Resource_Shen_Tong:
			startTaskID = 10035
			taskID = 10039 //神通升级
			taskCount = FortuneTaskMap[taskID].Count - FortuneTaskMap[taskID].Num
			if !FortuneTaskMap[taskID].Take && taskCount > 0 {
				log.Infof("%s-[%s]已完成%d次", fortuneName, FortuneTaskMap[taskID].Name, FortuneTaskMap[taskID].Num)
				for i = 0; i < taskCount; i++ {
					time.Sleep(3 * time.Second)
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_MAGIC_LEVEL_UP, "08 "+GetRandomMagicDerivationID()+" 10 00"))
				}
			}
			taskID = 10043 //神通衍化
			taskCount = FortuneTaskMap[taskID].Count - FortuneTaskMap[taskID].Num
			if !FortuneTaskMap[taskID].Take && taskCount > 0 {
				log.Infof("%s-[%s]已完成%d次", fortuneName, FortuneTaskMap[taskID].Name, FortuneTaskMap[taskID].Num)
				for i = 0; i < taskCount; i++ {
					time.Sleep(3 * time.Second)
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_MAGIC_DERIVATION, "08 01"))
				}
			}
		case D.R_Resource_Nei_Dan:
			startTaskID = 10052
		case D.R_Resource_Fa_Bao:
			startTaskID = 10069
			taskID = 10074 //寻法宝
			taskCount = FortuneTaskMap[taskID].Count - FortuneTaskMap[taskID].Num
			if !FortuneTaskMap[taskID].Take && taskCount > 0 {
				log.Infof("%s-[%s]已完成%d次", fortuneName, FortuneTaskMap[taskID].Name, FortuneTaskMap[taskID].Num)
				for i = 0; i < taskCount; i++ {
					time.Sleep(3 * time.Second)
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_MAGIC_TREASURE_DRAW_REQ, "08 01 10 00 18 01 28 E0 8D 06"))
				}
			}
			taskID = 10077 //炼化法宝
			taskCount = FortuneTaskMap[taskID].Count - FortuneTaskMap[taskID].Num
			if !FortuneTaskMap[taskID].Take && taskCount > 0 {
				log.Infof("%s-[%s]已完成%d次", fortuneName, FortuneTaskMap[taskID].Name, FortuneTaskMap[taskID].Num)
				for i = 0; i < taskCount; i++ {
					time.Sleep(3 * time.Second)
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_MAGIC_TREASURE_LV_UP_REQ, "08"+GetRandomMagicTreasureID()+" 10 00"))
				}
			}
		case D.A_Fortune_Unkown:

		}
		//抽奖次数
		if FortuneTaskMap[startTaskID+16].Done && FortuneTaskMap[startTaskID+16].Take {
			log.Infof("%s-领取抽奖200次礼包", fortuneName)
		} else if FortuneTaskMap[startTaskID+15].Done && FortuneTaskMap[startTaskID+15].Take {
			log.Infof("%s-领取抽奖120次礼包", fortuneName)
		} else if FortuneTaskMap[startTaskID+14].Done && FortuneTaskMap[startTaskID+14].Take {
			log.Infof("%s-领取抽奖100次礼包", fortuneName)
		} else if FortuneTaskMap[startTaskID+13].Done && FortuneTaskMap[startTaskID+13].Take {
			log.Infof("%s-领取抽奖50次礼包", fortuneName)
		} else if FortuneTaskMap[startTaskID+12].Done && FortuneTaskMap[startTaskID+12].Take {
			log.Infof("%s-领取抽奖30次礼包", fortuneName)
		}
	}
}
